package wordsCloud;

import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.CircleBackground;
import com.kennycason.kumo.bg.PixelBoundryBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.LinearFontScalar;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.nlp.tokenizers.ChineseWordTokenizer;
import com.kennycason.kumo.palette.ColorPalette;

import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.List;

@Service("WordCloud")
public class WordCloudUtils implements wordsCloud.WordCloud {
    public static final String In_PATH = "ruoyi-hzhtest/src/main/resources/";
    public static final String Out_PATH = "ruoyi-ui/src/assets/wordcloud/";

    @Override
    public List<WordFrequency> wordsSegment(String str) {

        return null;
    }

    public static List<WordFrequency> readWordFrequencies(String filePath) throws IOException {
        List<WordFrequency> wordFrequencies = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(" ");
                if (parts.length == 2) {
                    String word = parts[0];
                    int frequency = Integer.parseInt(parts[1]);
                    wordFrequencies.add(new WordFrequency(word, frequency));
                }
            }
        }
        return wordFrequencies;
    }

    @Override
    public String createSimpleImage() {
        // 图片名
        long timestamp = System.currentTimeMillis();
        String imgName = "Img_" + timestamp + ".png";
        System.out.println("imgName: " + imgName);


        final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        frequencyAnalyzer.setWordFrequenciesToReturn(300);
        frequencyAnalyzer.setMinWordLength(4);
        frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
//        frequencyAnalyzer.setStopWords(loadStopWords());

        try
        {
            // 从文件中读取词频数据
            final List<WordFrequency> wordFrequencies = this.readWordFrequencies(In_PATH + "Files/word_frequency.txt");
            System.out.println(wordFrequencies);

            final Dimension dimension = new Dimension(600, 600);
            final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
            wordCloud.setPadding(2);
            wordCloud.setBackground(new CircleBackground(300)); // 设置圆形背景
//            wordCloud.setBackground(new PixelBoundryBackground(In_PATH + "Images/whale_small.png"));
            wordCloud.setColorPalette(new ColorPalette(new Color(0x4055F1), new Color(0x408DF1), new Color(0x40AAF1), new Color(0x40C5F1), new Color(0x40D3F1), new Color(0xFFFFFF)));
            wordCloud.setFontScalar(new LinearFontScalar(20, 50));

            // 设置支持中文的字体
            Font font = new Font("SimHei", Font.PLAIN, 20); // SimHei 是一个常见的中文字体
            wordCloud.setKumoFont(new KumoFont(font));

            // 设置背景颜色为白色
            wordCloud.setBackgroundColor(Color.WHITE);

            // 创建词云
            wordCloud.build(wordFrequencies);
            wordCloud.writeToFile(Out_PATH + imgName);

            return imgName;

        }
        catch(Exception ex)
        {
            return "error";
        }
    }

    @Override
    public String createSimpleImage(String In) {
        // 图片名
        long timestamp = System.currentTimeMillis();
        String imgName = "Img_" + timestamp + ".png";
        System.out.println("imgName: " + imgName);


        final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        frequencyAnalyzer.setWordFrequenciesToReturn(300);
        frequencyAnalyzer.setMinWordLength(4);
        frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
//        frequencyAnalyzer.setStopWords(loadStopWords());

        try
        {
            // 从文件中读取词频数据
            final List<WordFrequency> wordFrequencies = this.readWordFrequencies(In);
            System.out.println(wordFrequencies);

            final Dimension dimension = new Dimension(600, 600);
            final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
            wordCloud.setPadding(2);
            wordCloud.setBackground(new CircleBackground(300)); // 设置圆形背景
//            wordCloud.setBackground(new PixelBoundryBackground(In + "Images/whale_small.png"));
            wordCloud.setColorPalette(new ColorPalette(new Color(0x4055F1), new Color(0x408DF1), new Color(0x40AAF1), new Color(0x40C5F1), new Color(0x40D3F1), new Color(0xFFFFFF)));
            wordCloud.setFontScalar(new LinearFontScalar(20, 50));

            // 设置支持中文的字体
            Font font = new Font("SimHei", Font.PLAIN, 20); // SimHei 是一个常见的中文字体
            wordCloud.setKumoFont(new KumoFont(font));

            // 设置背景颜色为白色
            wordCloud.setBackgroundColor(Color.WHITE);

            // 创建词云
            wordCloud.build(wordFrequencies);
            wordCloud.writeToFile(Out_PATH + imgName);

            return imgName;

        }
        catch(Exception ex)
        {
            return "error";
        }
    }


    @Override
    public String createSimpleImage(String In, String Image) {
        // 图片名
        long timestamp = System.currentTimeMillis();
        String imgName = "Img_" + timestamp + ".png";
        System.out.println("imgName: " + imgName);


        final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        frequencyAnalyzer.setWordFrequenciesToReturn(300);
        frequencyAnalyzer.setMinWordLength(4);
        frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
//        frequencyAnalyzer.setStopWords(loadStopWords());

        try
        {
            // 从文件中读取词频数据
            final List<WordFrequency> wordFrequencies = this.readWordFrequencies(In + "Files/word_frequency.txt");
            System.out.println(wordFrequencies);

            final Dimension dimension = new Dimension(600, 600);
            final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
            wordCloud.setPadding(2);
//            wordCloud.setBackground(new CircleBackground(150)); // 设置圆形背景
            wordCloud.setBackground(new PixelBoundryBackground(Image));
            wordCloud.setColorPalette(new ColorPalette(new Color(0x4055F1), new Color(0x408DF1), new Color(0x40AAF1), new Color(0x40C5F1), new Color(0x40D3F1), new Color(0xFFFFFF)));
            wordCloud.setFontScalar(new LinearFontScalar(20, 50));

            // 设置支持中文的字体
            Font font = new Font("SimHei", Font.PLAIN, 20); // SimHei 是一个常见的中文字体
            wordCloud.setKumoFont(new KumoFont(font));

            // 设置背景颜色为白色
            wordCloud.setBackgroundColor(Color.WHITE);

            // 创建词云
            wordCloud.build(wordFrequencies);
            wordCloud.writeToFile(Out_PATH + imgName);

            return imgName;

        }
        catch(Exception ex)
        {
            return "error";
        }
    }

    @Override
    public Collection<String> loadStopWords() {
        return null;
    }

    @Override
     public List<WordFrequency> readWordFrequencyFromFile(String filename) {
        List<WordFrequency> wordFrequencies = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split("\\s+");
                if (parts.length == 2) {
                    String word = parts[0];
                    int frequency = Integer.parseInt(parts[1]);
                    wordFrequencies.add(new WordFrequency(word, frequency));
                }
            }
        }
        catch (Exception exception) {

        }
        return wordFrequencies;
    }

}